package com.md.controller;

import com.md.entity.myStudent;
import com.md.service.studentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
@CrossOrigin
public class studentController {
    @Autowired
    private studentService studentService;

    private R success(Object data){
        return new R(200,data);
    }
    private R failure(String msg){
        return new R(500,msg);
    }
    @GetMapping()
    public R selectAll() {
        List<myStudent> students = studentService.selectAll();
        return students!=null && !students.isEmpty()?
                success(students):
                failure("暂无内容");
    }

    @GetMapping("/{id}")
    public R selectOne(@PathVariable String id) {
        myStudent student = studentService.selectOne(Integer.parseInt(id));
        return student!=null?
                success(student):
                failure("暂无内容");
    }

    @GetMapping(params = "studentName")
    public R selectByName(String studentName) {
        List<myStudent> students = studentService.selectByName(studentName);
        System.out.println("student = "+ students);
        System.out.println("name = "+studentName);
        return students!=null && !students.isEmpty()?
                success(students):
                failure("暂无内容");
    }

    @PostMapping()
    public R add(@RequestBody myStudent myStudent) {
        Boolean aBoolean  = studentService.add(myStudent);
        return aBoolean ? success("添加成功"):failure("添加失败");
    }

    @DeleteMapping ("/{id}")
    public R delete(@PathVariable String id) {
        Boolean aBoolean = studentService.delete(Integer.parseInt(id));
        return aBoolean ? success("删除成功"):failure("删除失败");
    }

    @PutMapping()
    public R change(@RequestBody myStudent myStudent) {
        Boolean aBoolean= studentService.change(myStudent);
        return aBoolean ? success("修改成功"):failure("修改失败");
    }
}
